package ge.telasi.tasks.ui.search;

import ge.telasi.tasks.search.Condition;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;

/**
 * Search panel.
 *
 * @author dimitri
 */
public class SearchPanel extends JPanel {
    private Field[] fields;

    public SearchPanel() {
        initComponents();
    }

    private void initComponents() {
        setLayout(new GridBagLayout());
    }

    public Field[] getFields() {
        return fields;
    }

    public void setFields(Field[] fields) {
        this.fields = fields;
    }

    public void addLine() {
        SearchLine line = new SearchLine();
        line.setFields(fields);
        line.setSearchPanel(this);
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.weightx = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        add(line, c);
        revalidate();
        doLayout();
    }

    public void deleteLine(SearchLine line) {
        remove(line);
        revalidate();
        doLayout();
    }

    public List<Condition> getConditions() {
        List<Condition> conditions = new ArrayList<Condition>();
        for (int i = 0; i < getComponentCount(); i++) {
            Component comp = getComponent(i);
            if (comp instanceof SearchLine) {
                SearchLine line = (SearchLine) comp;
                Condition condition = line.getCondition();
                if (condition != null) {
                    conditions.add(condition);
                }
            }
        }
        return conditions;
    }

}
